package org.wenhuayuan.project.main.demo

import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_demo_viewpager.*
import org.wenhuayuan.project.common.view.loadUrl
import org.wenhuayuan.project.main.R
import org.wenhuayuan.project.main.banner.indicator.CircleIndicator
import org.wenhuayuan.uilibrary.banner.core.HiBanner
import org.wenhuayuan.uilibrary.banner.core.HiBannerAdapter
import org.wenhuayuan.uilibrary.banner.core.HiBannerMo
import org.wenhuayuan.uilibrary.banner.core.IHiBanner
import java.util.*

class DemoViewpagerActivity : AppCompatActivity() {
    private var urls = arrayOf(
        "https://www.devio.org/img/beauty_camera/beauty_camera1.jpg",
        "https://www.devio.org/img/beauty_camera/beauty_camera3.jpg",
        "https://www.devio.org/img/beauty_camera/beauty_camera4.jpg",
        "https://www.devio.org/img/beauty_camera/beauty_camera5.jpg",
        "https://www.devio.org/img/beauty_camera/beauty_camera2.jpg",
        "https://www.devio.org/img/beauty_camera/beauty_camera6.jpg",
        "https://www.devio.org/img/beauty_camera/beauty_camera7.jpg",
        "https://www.devio.org/img/beauty_camera/beauty_camera8.jpeg"
    )
    private var autoPlay = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_demo_viewpager)

        initView(autoPlay, false)

        auto_play.setOnCheckedChangeListener { _, isChecked ->
            autoPlay = isChecked
            initView(isChecked, isChecked)
        }
        tv_switch.setOnClickListener {

        }
    }

    private fun initView(autoPlay: Boolean, loop: Boolean) {
        val hiBanner = findViewById<HiBanner>(R.id.banner)
        val list: MutableList<HiBannerMo> = ArrayList()
        for (i in 0..7) {
            val mo = HiBannerMo()
            mo.url = urls[i % urls.size]
            list.add(mo)
        }
        hiBanner.setIntervalTime(2000)
        hiBanner.setAutoPlay(autoPlay)
        hiBanner.setLoop(loop)
        hiBanner.setScrollDuration(2000)

        val circleIndicator = CircleIndicator(this)
        circleIndicator.setBannerMos(list)
        hiBanner.setHiIndicator(circleIndicator)
        hiBanner.setOnBannerClickListener(object: IHiBanner.OnBannerClickListener{
            override fun onBannerClick(
                viewHolder: HiBannerAdapter.HiBannerViewHolder,
                bannerMo: HiBannerMo,
                position: Int
            ) {
                Toast.makeText(this@DemoViewpagerActivity, bannerMo.url, Toast.LENGTH_LONG).show()
            }

            override fun onBannerLongClick(
                viewHolder: HiBannerAdapter.HiBannerViewHolder,
                bannerMo: HiBannerMo,
                position: Int
            ) {
                Toast.makeText(this@DemoViewpagerActivity, "长按", Toast.LENGTH_LONG).show()
            }
        })
        hiBanner.setBannerData(R.layout.banner_item_layout, list)
        hiBanner.setBindAdapter { viewHolder, mo, _ ->
            val textView = viewHolder.findViewById<TextView>(R.id.tv_title)
            val imageView = viewHolder.findViewById<ImageView>(R.id.iv_image)
            imageView.loadUrl(mo.url)
//            Glide.with(this@DemoViewpagerActivity).load(mo.url).into(imageView)
            textView.text = mo.url
            Log.i("DemoViewpagerActivity", "hibanner.setBindAdapter = ${mo.url}")
        }


    }
}
